package com.example.taxfloatproject20240926.ui;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.widget.Toast;

import com.example.taxfloatproject20240926.ui.busevent.BusProvider;
import com.example.taxfloatproject20240926.ui.event.ConnectNetEvent;


/**
 * Created by Administrator on 2019/12/28.
 */

public class ConnectNetReciver extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {
        ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo networkInfo = connectivityManager.getActiveNetworkInfo();
        if(networkInfo!=null && networkInfo.getType()== ConnectivityManager.TYPE_WIFI){
//            Toast.makeText(context,"now is wifi",Toast.LENGTH_SHORT).show();
            BusProvider.getInstance().post(new ConnectNetEvent(true));
        }
        else if(networkInfo!=null && networkInfo.getType()== ConnectivityManager.TYPE_MOBILE) {
//            Toast.makeText(context, "now is 移动数据", Toast.LENGTH_SHORT).show();
            BusProvider.getInstance().post(new ConnectNetEvent(true));
        }else if (networkInfo!=null&&networkInfo.getType()== ConnectivityManager.TYPE_ETHERNET){
//            Toast.makeText(context, "有线数据", Toast.LENGTH_SHORT).show();
            BusProvider.getInstance().post(new ConnectNetEvent(true));
        }
        else{
            Toast.makeText(context,"没有网络", Toast.LENGTH_SHORT).show();
            BusProvider.getInstance().post(new ConnectNetEvent(false));
        }


    }
}
